package quq.missq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.PhotoInAlbumActivity;
import quq.missq.activity.UpdateAlbumActivity;
import quq.missq.beans.Album;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/27/15.
 */
public class TaAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements VolleyTool.HTTPListener{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<Album> results;
    private LayoutInflater mInflater;
    private AlertDialog dialog;


    public TaAlbumAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<Album> results){
        this.results = results;
    }

    public LinkedList<Album> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_SUBSCRIBLE){
            View view = mInflater.inflate(R.layout.album_adapter,null);
            return new AtMeHolder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof AtMeHolder){
            AtMeHolder holder = (AtMeHolder)viewHolder;
            final Album result = results.get(position);
            holder.albumName.setText(result.getName());
            ImageLoadUtil.loadImage(holder.albumCover,Constants.IMAGE_HOST+result.getCover(),R.mipmap.empty_photo);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, PhotoInAlbumActivity.class);
                    intent.putExtra("id",result.getId());
                    intent.putExtra("type",5);
                    mContext.startActivity(intent);
                }
            });
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    LayoutInflater inflater = LayoutInflater.from(mContext);
//                    View view = inflater.inflate(R.layout.album_modify, null);
//                    final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//                    builder.setView(view);
//                    dialog = builder.create();
//                    ((TextView)view.findViewById(R.id.delete_album)).setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            deleteAlbum(result);
//
//                        }
//                    });
//                    ((TextView)view.findViewById(R.id.update_album)).setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Intent intent = new Intent();
//                            Bundle bundle = new Bundle();
//                            bundle.putSerializable("album",result);
//                            intent.putExtras(bundle);
//                            intent.putExtra("title","修改相册");
//                            intent.setClass(mContext,UpdateAlbumActivity.class);
//                            ((Activity)mContext).startActivityForResult(intent,0);
//                        }
//                    });
//                    dialog.show();
//                    return false;
//                }
//            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_SUBSCRIBLE;
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class AtMeHolder extends RecyclerView.ViewHolder{

        ImageView albumCover;
        TextView albumName;

        public AtMeHolder(View itemView) {
            super(itemView);
            albumCover = (ImageView)itemView.findViewById(R.id.albumCover);
            albumName = (TextView)itemView.findViewById(R.id.albumName);
            ((Activity)mContext).registerForContextMenu(itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

    public void deleteAlbum(Album album){
        Map<String,String> params = new HashMap<>();
        params.put("acc_token", UserTools.getUser(mContext).getAcc_token());
        params.put("id",album.getId()+"");
        VolleyTool.get((Activity) mContext, Constants.DELETE_ALBUM_URL, params, this, Constants.DELETE_ALBUM, null);
        results.remove(album);
        TaAlbumAdapter.this.notifyDataSetChanged();
        if(dialog != null){
            dialog.dismiss();
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        try {
            JSONObject json = new JSONObject(t.toString());
            int code = json.getInt("code");
            String msg = json.getString("message");
            if(code >= 0){
                Toast.makeText(mContext,"删除成功",Toast.LENGTH_SHORT).show();
            }else{}
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }


}
